//
//  HomeTableViewCell.m
//  Bread-Live
//
//  Created by ios on 2017/3/17.
//  Copyright © 2017年 Chris. All rights reserved.
//

#import "HomeTableViewCell.h"
#import "LiveTimeView.h"

@interface HomeTableViewCell ()

@property(nonatomic,strong) UIImageView *bgImageView;
@property(nonatomic,strong) UIImageView *headImageView;
@property(nonatomic,strong) UILabel     *titleLab;
@property(nonatomic,strong) UIButton    *numberBtn;//实际上作为一个label 的效果就是
@property(nonatomic,strong) UILabel     *nameLabel;
@property(nonatomic,strong) LiveTimeView *timeView;

@end

@implementation HomeTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self configView];
    }
    return self;
}


-(void)configView{
    CGFloat viewW = self.frame.size.width;
 // CGFloat viewH = self.frame.size.height;
    
    CGFloat bgImgH = (kWidth)/2;
    _bgImageView    = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewW, bgImgH)];
    _bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    _bgImageView.clipsToBounds = YES;
    _bgImageView.layer.masksToBounds = YES;
    [self.contentView addSubview:_bgImageView];
    
    CGFloat headImgW = 45;
    CGFloat headImgH = 45;
    
    _headImageView   = [[UIImageView alloc] initWithFrame:CGRectMake(viewW-10-headImgW , (bgImgH*2-headImgH)/2,headImgW,headImgH)];
    
    _headImageView.layer.cornerRadius  = headImgW/2;
    
    _headImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    _headImageView.layer.borderWidth = 1.0;
    _headImageView.layer.masksToBounds = YES;
    
    [self.contentView addSubview:_headImageView];
    
    _nameLabel     = [[UILabel alloc] initWithFrame:CGRectMake(viewW-10-headImgW, kWidth/2+15, headImgW, 42)];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.font          = [UIFont wyNormalFot:10];
    _nameLabel.textColor     = TEXT_COLOR;
    _nameLabel.contentMode   = UIViewContentModeTop;
    _nameLabel.numberOfLines = 0;
    
    [self.contentView addSubview:_nameLabel];
    
    _titleLab  = [[UILabel alloc] initWithFrame:CGRectMake(10, bgImgH, viewW-10-headImgW-10, 21)];
    _titleLab.textColor   =  WYCOLOR(86, 86, 86, 1.0);
    _titleLab.font        =  [UIFont wyBlodFont:16];
    _titleLab.numberOfLines = 2;
    
    [self.contentView addSubview:_titleLab];
    
    _timeView = [[LiveTimeView alloc] init];

    [self.contentView addSubview:_timeView];
    
    _numberBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //_numberBtn.frame  = CGRectMake(width+25+10+20, bgImgH+35, 30, 20);
    [_numberBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    _numberBtn.titleLabel.font = [UIFont wyNormalFot:13];
    [_numberBtn setTitleColor:TEXT_COLOR forState:UIControlStateNormal];
    
    [self.contentView addSubview:_numberBtn];
    
    
}

-(void)setModel:(HomeListModel *)model{
    CGFloat viewW = self.frame.size.width;
 //   CGFloat viewH = self.frame.size.height;
    
    CGFloat bgImgH = (kWidth)/2 ;
    
    [_bgImageView sd_setImageWithURL:[NSURL URLWithString:model.bgUrl]];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:model.headImgUrl]];
    
    _titleLab.text = model.title;
    
    _nameLabel.text = model.name;
    
    [_numberBtn setTitle:model.fansNumber forState:UIControlStateNormal];
    
    CGFloat titleH = [ToolsCustom getSpaceLabelHeight:model.title withFont:[UIFont wyBlodFont:16] withWidth:kWidth-10-45-10 lineSpacing:2];
    
    if (titleH > 21) {
        _titleLab.frame  = CGRectMake(10, bgImgH, viewW-10-45-10, 42);
        
    }else{
        _titleLab.frame  = CGRectMake(10, bgImgH, viewW-10-45-10, 21);
    }
    
    CGFloat width = [ToolsCustom setLabelText:model.time textFont:[UIFont wyNormalFot:13]];
    
    _timeView.frame = CGRectMake(10, bgImgH+(_titleLab.frame.size.height)+10, width+25, 25);
    
    [_timeView setliveTimeTitle:model.time liveStatus:YES];
    
    _numberBtn.frame = CGRectMake(width+25+10+20,bgImgH+(_titleLab.frame.size.height)+10, 30, 20);
    
}

- (void)setFrame:(CGRect)frame
{
    //修改cell的左右边距为10;
    //修改cell的Y值下移10;
    //修改cell的高度减少10;
    static CGFloat margin = 5;
    frame.origin.x     = margin;
    frame.size.width  -= 2 * frame.origin.x;
    frame.origin.y    += margin;
    frame.size.height -= margin;
    
    [super setFrame:frame];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
